package com.ecp.templet;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ecp.base.service.UserService;
import com.ecp.crm.bean.User;
import com.ecp.util.WebUtil;

/** * @author  wangyin: 
    * @date    2015年10月28日 上午10:37:14
    */
@Controller
public class HeaderController {
	private final String	gobalFileUrl	= "/front/commPart/gobalFile";
	private final String	successView		= "/front/commPart/header";
	private final String	loginInfo		= "/front/commPart/loginInfo";

	@Autowired
	UserService				userService;

	//全局头
	@RequestMapping("/globalFile.do")
	public ModelAndView getGobalFile(HttpServletRequest req, HttpServletResponse resp) {
		Map<String, Object> model = new HashMap<String, Object>();
		String isMini = "yes".equals(req.getParameter("isMini")) ? "yes" : "no";
		model.put("isMini", isMini);
		return new ModelAndView(this.gobalFileUrl, model);

	}

	//请求头部信息
	@RequestMapping("/header.do")
	public ModelAndView getHeader(HttpServletRequest req, HttpServletResponse resp)
			throws JSONException {

		Map<String, Object> model = new HashMap<String, Object>();

		try {
			//获取是否为首页
			String isIndex = "yes".equals(req.getParameter("isIndex")) ? "yes" : "no";
			model.put("isIndex", isIndex);

			//获取是否是迷你header
			String isMini = "yes".equals(req.getParameter("isMini")) ? "yes" : "no";
			model.put("isMini", isMini);

			//获取是否是下载中心 下载中心
			String loadDown = "yes".equals(req.getParameter("loadDown")) ? "yes" : "no";
			model.put("loadDown", loadDown);

			//获取用户登录信息
			User user = WebUtil.getFrontUser(req, userService);
			String loginId = user == null ? null : user.getLoginId();
			model.put("userId", loginId);

			//是否显示VIP专区栏目连接
			int displayVipCol = 0;
			if (user != null && user.getIsUsable() >= User.ISUSABLE_ROLMEX_OLD) {
				displayVipCol = 1;
			}
			model.put("displayVipCol", displayVipCol);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView(this.successView, model);

	}

	@RequestMapping("/dynamicLgoinInfo.do")
	public ModelAndView dynamicLgoinInfo(HttpServletRequest req, HttpServletResponse resp) {
		Map<String, Object> model = new HashMap<String, Object>();
		try {
			User user = WebUtil.getFrontUser(req, userService);
			if (user != null) {
				model.put("userId", user.getLoginId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView(this.loginInfo, model);
	}
}
